package cn.edu.neusoft.demo.hospital2.Controller;

import cn.edu.neusoft.demo.hospital2.pojo.Notice;
import cn.edu.neusoft.demo.hospital2.Service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/all")
    public Map<String, Object> getAllNotices() {
        Map<String, Object> result = new HashMap<>();
        List<Notice> list = noticeService.getAllNotices();
        result.put("code", 200);
        result.put("data", list);
        result.put("message", "查询成功");
        return result;
    }

    @GetMapping("/{id}")
    public Map<String, Object> getNoticeById(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        Notice notice = noticeService.getNoticeById(id);
        result.put("code", 200);
        result.put("data", notice);
        result.put("message", "查询成功");
        return result;
    }

    @PostMapping("/add")
    public Map<String, Object> addNotice(@RequestBody Notice notice) {
        Map<String, Object> result = new HashMap<>();
        int res = noticeService.addNotice(notice);
        if (res > 0) {
            result.put("code", 200);
            result.put("message", "新增成功");
        } else {
            result.put("code", 500);
            result.put("message", "新增失败");
        }
        return result;
    }

    @PutMapping("/update")
    public Map<String, Object> updateNotice(@RequestBody Notice notice) {
        Map<String, Object> result = new HashMap<>();
        int res = noticeService.updateNotice(notice);
        if (res > 0) {
            result.put("code", 200);
            result.put("message", "更新成功");
        } else {
            result.put("code", 500);
            result.put("message", "更新失败");
        }
        return result;
    }

    @DeleteMapping("/delete/{id}")
    public Map<String, Object> deleteNotice(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        int res = noticeService.deleteNotice(id);
        if (res > 0) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "删除失败");
        }
        return result;
    }
}